﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace syc.Store
{
    public partial class 备件到货记录管理 : System.Web.UI.Page
    {
        string sql =
@"SELECT t1.ID,t2.品名,t2.规格,t2.单位,t1.数量,cast(t1.单价 as float) as 单价,t1.厂商,t1.日期
,cast(t1.计划年度 as varchar)+'-'+ (case when t1.计划月份<10 then '0'+cast(t1.计划月份 as varchar) else cast(t1.计划月份 as varchar) end) as 计划
FROM 备件到货 t1 left outer join 备品备件 t2 on t2.ID=t1.类别ID";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql + " order by ID desc";
                this.SqlText.Text = this.SqlDataSource1.SelectCommand;
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
            this.Button_View.Enabled = false;
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectedID.Text = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_View.Enabled = true;
            this.Button_View.OnClientClick = "parent.swin2_load('/Store/备件到货记录.aspx?id=" + this.SelectedID.Text + "',760,600,'yes');return false;";

            this.Button_Edit.Enabled = true;
            this.Button_Edit.OnClientClick = "parent.swin2_load('/Store/备件到货记录录入.aspx?id=" + this.SelectedID.Text + "',600,350);return false;";

            this.Button_Delete.Enabled = true;
        }
        private string GetState()
        {
            string state = "";

            if (this.NianDu.Text != "0")
            {
                state += " and t1.计划年度=" + this.NianDu.Text;
            }
            if (this.YueFen.Text != "0")
            {
                state += " and t1.计划月份=" + this.YueFen.Text;
            }

            if (this.PinMing.Text != "")
            {
                state += " and t2.品名 like '%" + this.PinMing.Text + "%'";
            }
            if (this.ChangShang.Text != "")
            {
                state += " and t1.厂商 like '%" + this.ChangShang.Text + "%'";
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            this.SqlText.Text = sql + state + " order by ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.NianDu.Text = "0";
            this.YueFen.Text = "0";
            this.PinMing.Text = "";
            this.ChangShang.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "delete from 备件到货 where ID=" + this.SelectedID.Text;
            this.SqlDataSource1.Delete();
        }
    }
}
